﻿using System.Collections.Generic;
using CarlosAg.ExcelXmlWriter;
using  bfs.sms.sdmx.util.Variable;

namespace bfs.sms.sdmx.util.Excel
{
    /// <summary> Excel XML Variable catalog generator </summary>
    public class ExcelCatalog : ExcelBasicCatalog
    {
        #region Internal members

        private string codelistBookName;

        #endregion

        #region C'tor

        public ExcelCatalog(string author, string company, string codesBookName)
            : base(author, company)
        {
            codelistBookName = codesBookName;
        }

        #endregion

        #region Generation


        /// <summary> Generation of one worksheet of the variable book</summary>
        /// <param name="varSheet">object representing list of variable concepts of the sheet</param>
        protected override void GenerateWorksheet(VariableSheet varSheet)
        {
            int headerRowCount = 3;

            // TOC entry
            tocSet.Add(new TableOfContentInfo(varSheet.RootConcept.Id, varSheet.RootConcept.Name.GetForLanguage(langCd)));
            
            // Excel Sheet creation
            sheet = book.Worksheets.Add(varSheet.RootConcept.Id);
            sheet.Table.DefaultColumnWidth = 60F;
            sheet.Table.ExpandedColumnCount = 6;
            sheet.Table.ExpandedRowCount = varSheet.VarList.Count + headerRowCount;
            sheet.Table.FullColumns = 1;
            sheet.Table.FullRows = 1;

            // Lock top rows
            SplitSheetHorizontal(sheet, 3);

            // Columns formats  override sizes
            WorksheetColumn columnVar = sheet.Table.Columns.Add();
            columnVar.Index = 1;
            columnVar.Width = catalogOptions.VarIdPixels;

            WorksheetColumn columnText = sheet.Table.Columns.Add();
            columnText.Index = 2;
            columnText.Width = catalogOptions.VarNamePixels;

            // Header info
            List<string> header = new List<string>(new string[] { "Variable", "Text", "Type", " Length", "Codes"});
            // Filtering range
            CreateNamedRange(sheet, 3, varSheet.VarList.Count, header.Count + 1);

            WorksheetRow rowHead = AddHeaderInfo(varSheet.RootConcept.Id, varSheet.RootConcept.Name.GetForLanguage(langCd), varSheet.RootConcept.Description.GetForLanguage(langCd), header, true);

            // Append Quality head link
            WorksheetColumn columnQuality = sheet.Table.Columns.Add();
            columnQuality.Index = 6;
            columnQuality.Width = 170;
            AddCellHyperlink(rowHead, @".\", codelistBookName, QualityHeaderName, QualitySheetId, "A1", CELL_STYLE_HLINK_HEADER, true);
           
            // Fill with all variables
            foreach (Variable.Variable var in varSheet.VarList)
                FillRow(var, codelistBookName);
        }

        /// <summary>Fill Excel row with concept variable informations </summary>
        /// <param name="var">Concept variable infos</param>
        /// <param name="codelistBookName">Excel codelist annexe book name required for links</param>
        protected void FillRow(Variable.Variable var, string codelistBookName)
        {
            WorksheetRow row = sheet.Table.Rows.Add();
            AddCell(row, var.Concept.Id, true);
            AddCell(row, var.Concept.Name.GetForLanguage(langCd), true);
            AddCell(row, var.FormatType.TextType.ToString(), true);
            string length = (string.IsNullOrEmpty(var.FormatType.maxLength) ? string.Empty : var.FormatType.maxLength.ToString());
            AddCell(row, length, true);

            // Associate codelist ?
            string codelistId = var.Codelist != null ? var.CodeSheetId : string.Empty;
            AddCellHyperlink(row, @".\", codelistBookName, codelistId, codelistId, "A1", CELL_STYLE_HLINK, true);

            // List of quality codes
            string listQuality = string.Empty;
            if (var.Qualitylist != null)
                foreach (SdmxMl.Structure.CodeType c in var.Qualitylist.CodeList)
                    listQuality += c.Id + ", ";
            listQuality = listQuality.Trim();
            listQuality = listQuality.TrimEnd(',');
            AddCell(row, listQuality, true);

        }

        #endregion

    }
}
